package com.rongxun.common.base.mvvm;


import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.rongxun.api.utils.FlyLog;
import com.rongxun.common.base.mvvm.view.IBaseView;
import com.rongxun.common.base.mvvm.viewmodel.BaseViewModel;

import java.util.Map;

import javax.annotation.Nullable;

import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.lifecycle.ViewModelProviders;


/**
 * 创建者     forza
 * 创建时间   2019/11/29 18:09
 * 描述
 */
public abstract class BaseMvvmFragment<V extends ViewDataBinding, VM extends BaseViewModel> extends BaseRxFragment implements IBaseView {
    protected V   mBinding;
    protected VM  mViewModel;
    private   int viewModelId;


    protected View initConentView(ViewGroup container) {
        ViewDataBinding dataBinding = DataBindingUtil.inflate(LayoutInflater.from(activity), onBindLayout(), container, false);
        initViewModel();
        initBaseViewObservable();
        initViewObservable();
        return dataBinding.getRoot();

    }

    protected abstract int onBindLayout();


    private void initViewModel() {
        mViewModel = createViewModel();
        viewModelId = onBindVariableId();
        if (mBinding != null) {
            mBinding.setVariable(viewModelId, mViewModel);
        }
        getLifecycle().addObserver(mViewModel);
    }

    private VM createViewModel() {
        return ViewModelProviders.of(this, onBindViewModelFactory()).get(onBindViewModel());
    }

    protected abstract ViewModelProvider.Factory onBindViewModelFactory();

    protected abstract Class<VM> onBindViewModel();

    protected abstract int onBindVariableId();


    private void initBaseViewObservable() {
        mViewModel.getUIChangeLiveData().getShowInitLoadViewEvent().observe(this, new Observer<Boolean>() {
            @Override
            public void onChanged(@Nullable Boolean show) {
                showInitLoadView(show);
            }
        });
        mViewModel.getUIChangeLiveData().getShowTransLoadingViewEvent().observe(this, new Observer<Boolean>() {
            @Override
            public void onChanged(@Nullable Boolean show) {
                FlyLog.d("view postShowTransLoadingViewEvent start...");
                showTransLoadingView(show);
            }
        });
        mViewModel.getUIChangeLiveData().getShowNoDataViewEvent().observe(this, new Observer<Boolean>() {
            @Override
            public void onChanged(@Nullable Boolean show) {
                showNoDataView(show);
            }
        });
        mViewModel.getUIChangeLiveData().getShowNetWorkErrViewEvent().observe(this, new Observer<Boolean>() {
            @Override
            public void onChanged(@Nullable Boolean show) {
                showNetWorkErrView(show);
            }
        });
        mViewModel.getUIChangeLiveData().getStartActivityEvent().observe(this, new Observer<Map<String, Object>>() {
            @Override
            public void onChanged(@Nullable Map<String, Object> params) {
                Class<?> clz = (Class<?>) params.get(BaseViewModel.ParameterField.CLASS);
                Bundle bundle = (Bundle) params.get(BaseViewModel.ParameterField.BUNDLE);
                startActivity(clz, bundle);
            }
        });
        mViewModel.getUIChangeLiveData().getFinishActivityEvent().observe(this, new Observer<Void>() {
            @Override
            public void onChanged(@Nullable Void v) {
                if (activity != null) {
                    activity.finish();
                }
            }
        });
        mViewModel.getUIChangeLiveData().getOnBackPressedEvent().observe(this, new Observer<Void>() {
            @Override
            public void onChanged(@Nullable Void v) {
                if (activity != null) {
                    activity.onBackPressed();
                }
            }
        });
    }

    private void startActivity(Class<?> clz, Bundle bundle) {
        Intent intent = new Intent(activity, clz);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivity(intent);
    }


    protected abstract void initViewObservable();


    @Override
    public void finishActivity() {
        activity.finish();
    }
}
